// import React from "react";
// import ReactDOM from "react-dom/client";
// import App from "./App";

// const root = ReactDOM.createRoot(document.getElementById("root"));
// root.render(<App />);

import { createStore } from "redux";

// 创建仓库
// reducer函数接收两个参数，第一个参数是state状态，actions用来指定做哪些操作
const initState = {
  count: 0,
};
function reducer(state = initState, action) {
  switch (action.type) {
    case "increment":
      let inCount = state.count + action.count;
      return { ...state, count: inCount };
    case "decrement":
      let deCount = state.count - action.count;
      return { ...state, count: deCount };
    default:
      return state;
  }
}

const store = createStore(reducer);

// 仓库提供了检测数据发生改变的方法
store.subscribe(() => {
  // 仓库提供了获取数据的方法 getState
  console.log(store.getState());
});

// 仓库提供了操作数据的方法 dispatch
store.dispatch({ type: "increment", count: 10 });

store.dispatch({ type: "increment", count: 10 });

store.dispatch({ type: "decrement", count: 5 });
